package gamedata

import (
	"encoding/csv"
	"fmt"
	"log"
	"os"
)

type ShoppingRecord struct {
	Id   string `json:"Id"`
	Name string `json:"Name"`
}

var (
	MergeCfgData []ShoppingRecord
)

func createShoppingList(data [][]string) []ShoppingRecord {
	var shoppingList []ShoppingRecord
	for i, line := range data {
		if i > 3 {
			var rec ShoppingRecord
			for j, field := range line {
				if j == 0 {
					rec.Id = field
				} else if j == 5 {
					rec.Name = field
				}
			}
			shoppingList = append(shoppingList, rec)
		}
	}
	return shoppingList
}

func LoadMergeData() {
	f, err := os.Open("gamedata/MergeData.csv")
	if err != nil {
		log.Fatal(err)
	}
	// remember to close the file at the end of the program
	defer f.Close()
	// read csv values using csv.Reader
	csvReader := csv.NewReader(f)
	csvReader.Comma = '\t'
	data, err := csvReader.ReadAll()
	if err != nil {
		log.Fatal(err)
	}
	shoppingList := createShoppingList(data)
	// print the array
	fmt.Printf("%+v", shoppingList)
	MergeCfgData = shoppingList
}
